/*
 * Filename : SOCInputValidator.java 
 * Package : com.spi.soconversion.util
 * Created by: Software Paradigms International Group, LLC. (SPI) 
 * Copyright (c) 2010 HBC
 */

package com.spi.soconversion.util;

import com.spi.soconversion.base.SOCException;

/**
 * This class validates the inputs and displayed appropriate messages
 * <p>
 * Issues:
 * <p>
 * NONE
 * <p>
 * History:
 * <p>
 * <ul>
 * <li>2010.05.21 pk: created
 * </ul>
 *
 * @author Priya Khanna, SPI (pk)
 */

public class InputValidator {

	/**
	 * Variable referring to the path of the xml files
	 */
	private String d_xmlDir = null;
	
	/**
	 * Variable referring to the path of log files
	 */
	private String d_logDir = null;
	
	/**
	 * Variable referring to the host
	 */
	private String d_host= null;
	
	/**
	 * Variable referring to the service
	 */
	private String d_service = null;
	
	/**
	 * Variable referring to the user 
	 */
	private String d_user= null;
	
	/**
	 * Variable referring to the Password
	 */
	private String d_pass = null;
	
	/**
	 * Variable referring to the batch size
	 */
	private int d_batchSize ;

	/**
	 * Variable referring to flag indicating data fetch for install base
	 */
	private String d_fetchData = "N";
	
	/**
	 * Constant indicates the fixed size of all input parameters
	 */
	private static final int d_fixedSize = 3;

	/**
	 * Constructor Initialization
	 */
	public InputValidator()
	{
		super();
	}
	/**
	 * This method is used for displaying the correct format of
	 * parameters to be passed in the Executable file  
	 */
	public void usage()
	{
		
		String result =
			"\n"
			+ "[USAGE] \n "
			+ "com.spi.soconversion.SOCInstallBaseController \n "
			+ "-fd                     Flag For Data Loading\n "
			+ "-xd<XML Dir>            Full Path of the XML base Directory \n "
			+ "-ld<Log Dir>            Full Path of the log Directory \n "
			+ "-u1 <username>          User Name for Schema  \n "
			+ "-p1<password>           Password for Schema  \n "
			+ "-s1<serviceName>        Databse ServiceName \n "
			+ "-h1<hostName>           Databse Host Name  \n"
			+ "-bs<batch size>         Batch Size for xml creation \n";

		System.out.println(result);
	}

	/**
	 * This method validates the various inputs and 
	 * does Exception handling
	 * @param args - arguments passed to the Application
	 * @throws SOCException - Wraps up all the exceptions occurred while 
	 * validations of Inputs 
	 */
	public void validateInputs(String[] args) throws SOCException
	{
		try
		{
			if (args!=null)
			{
				int noOfArguments = args.length;
				if(0 == noOfArguments)
				{
					throw new SOCException(
	                		ErrorMessageReader.getError(
	                				ErrorConstants.MISSING_INPUT_PARAMETERS));
				}
				else
				{
						if (noOfArguments !=8)
						{
							throw new SOCException(
			                		ErrorMessageReader.getError(
			                				ErrorConstants.INVALID_INPUT_PARAMETERS));
						}
						}
						for (int loopCounter = 0; loopCounter < noOfArguments; loopCounter++)
						{
							String tempStr = args[loopCounter];
							if (tempStr.startsWith("-xd"))
							{
								d_xmlDir = tempStr.substring(d_fixedSize);
								if(!SOCUtility.checkDirectory(d_xmlDir))
					    		{
					    			throw new SOCException(
					                		ErrorMessageReader.getError(
					                				ErrorConstants.INVALID_OUTPUT_DIRECTORY)+d_xmlDir);
					    		}
							}	
							else if (tempStr.startsWith("-ld"))
							{
								d_logDir = tempStr.substring(d_fixedSize);
								if(!SOCUtility.checkDirectory(d_logDir))
					    		{
					    			throw new SOCException(
					                		ErrorMessageReader.getError(
					                				ErrorConstants.INVALID_OUTPUT_DIRECTORY)+d_logDir);
					    		}
							}		
							else if (tempStr.startsWith("-u1"))
							{

								d_user = tempStr.substring(d_fixedSize);
								if (d_user.equalsIgnoreCase(SOCConstants.EMPTY_STRING))
								{
									throw new SOCException(
					                		ErrorMessageReader.getError(
					                				ErrorConstants.MISSING_DATABASE_USER));
								}

							}
							else if (tempStr.startsWith("-p1"))
							{

								d_pass= tempStr.substring(d_fixedSize);
								if (d_pass.equalsIgnoreCase(SOCConstants.EMPTY_STRING))
								{
									throw new SOCException(
					                		ErrorMessageReader.getError(
					                				ErrorConstants.MISSING_DATABASE_PASSWORD));
								}
							}
							else if (tempStr.startsWith("-h1"))
							{

								d_host = tempStr.substring(d_fixedSize);
								if (d_host
										.equalsIgnoreCase(SOCConstants.EMPTY_STRING))
								{
									throw new SOCException(
					                		ErrorMessageReader.getError(
					                				ErrorConstants.MISSING_DATABASE_HOST));
								}
							}
							else if (tempStr.startsWith("-s1"))
							{
								d_service = tempStr.substring(d_fixedSize);
								if (d_service
										.equalsIgnoreCase(SOCConstants.EMPTY_STRING))
								{
									throw new SOCException(
					                		ErrorMessageReader.getError(
					                				ErrorConstants.MISSING_DATABASE_SERVICE));
								}
							}
							else if (tempStr.startsWith("-bs"))
							{
								try {

					            	d_batchSize=Integer.parseInt(tempStr.substring(d_fixedSize));
					            
					            } catch (NumberFormatException nfe) {
					            	throw new SOCException(
					                		ErrorMessageReader.getError(
					                				ErrorConstants.INVALID_BATCH_SIZE),nfe);
					            }
					            if(d_batchSize<=0)
					            {
					            	throw new SOCException(
					                		ErrorMessageReader.getError(
					                				ErrorConstants.INVALID_BATCH_SIZE_NOT_POSITIVE_NUMBER));
					            }
							}
							else if (tempStr.startsWith("-fd"))
							{
								d_service = tempStr.substring(d_fixedSize);
								if (d_service
										.equalsIgnoreCase(SOCConstants.EMPTY_STRING))
								{
									throw new SOCException(
					                		ErrorMessageReader.getError(
					                				ErrorConstants.MISSING_DATABASE_SERVICE));
								}
							}
							
						}						
			}
			else
			{
				throw new SOCException("NO Arguements for Validation");
			}	

		}
		catch(SOCException e)
		{
			usage();
			throw e;
		}
	}	
	
	public String getXMLDir()
	{
		return d_xmlDir;
	}
	
	public String getLogDir()
	{
		return d_logDir;
	}

	public String getUser()
	{
		return d_user;
	}
	
	public String getPassword()
	{
		return d_pass;
	}
	
	public String getHost()
	{
		return d_host;
	}
	
	public String getService()
	{
		return d_service;
	}
	
	public String getFetchData(){
		return d_fetchData;
	}
	
	public int getBatchSize(){
		return d_batchSize;
	}
}
